<?php
  /***************************************************************
   * @file: model.php
   * @author: Neo
   * @mail: xq262144@gmail.com
   * @date: 
   * @description:
   * this file is the model include of the framework
   ***************************************************************/
   
  if (!defined('SYS_BASEPATH')) {echo "No script access allowed!"; header("HTTP/1.0 404 Not Found"); } 
  
    class Model extends Base
    {
        private $table_name = null;
        
        public function __construct(){
            parent::__construct();
        }
        
        protected function setTableName($name = null){
            if (empty($name)) return false;
            else{
                $this->table_name = TB_PREFIX . $name;
                return true;
            }
        }
        
        public function getTableName(){
            return $this->table_name;
        }

        public function insert($info){
            if (empty($this->table_name)) throw new Exception('table_name var not set!');
            if (!is_array($info) || empty($info))
                return false;
            else{
                $sql = 'insert into ' . $this->table_name;            
                $cnt = count($info);
                $keys = ' (' . implode(', ', array_keys($info)) . ') ';
                $values = ' values({' . implode('}, {', range(0, $cnt - 1)) . '})';
                $sql .= $keys;
                $sql .= $values;
                return $this->db->query($sql, array_values($info)) ? $this->db->insertId() : false;
            }
        }
        
        public function delete($condition = null, $conn = 'and'){
            if (empty($this->table_name)) throw new Exception('table_name var not set!');
            $sql = 'delete from ' . $this->table_name . ' where ';
            if (is_array($condition) && !empty($condition)){
                $keys = array_keys($condition);
                $cnt = count($keys);
                while ($cnt --> 0){
                    $sql .= $keys[$cnt];
                    $sql .= " = {";
                    $sql .= "$cnt}";
                    if (0 != $cnt){
                        $sql .= " $conn ";
                    }
                }
                return $this->db->query($sql, array_values($condition));
            }
            elseif(null == $condition){
                $sql .= 'TRUE';
                return $this->db->query($sql);
            }
            else{
                return false;
            }
        }
        
        public function update($change, $condition = null, $conn = 'and'){
            if (empty($this->table_name)) throw new Exception('table_name var not set!');
            if (!is_array($change) || empty($change)){
                return false;
            }
            $sql = 'update ' . $this->table_name . ' set ';
            $change_keys = array_keys($change);
            $change_cnt = $cnt = count($change_keys);
            while ($cnt --> 0){
                $sql .= $change_keys[$cnt];
                $sql .= " = {";
                $sql .= "$cnt}";
                if (0 != $cnt){
                    $sql .= ", ";
                }
            }
            
            $sql .= " where ";
            
            if (is_array($condition) && !empty($condition)){
                $keys = array_keys($condition);
                $cnt = count($keys);
                while ($cnt --> 0){
                    $sql .= $keys[$cnt];
                    $sql .= " = {";
                    $sql .= ($cnt + $change_cnt);
                    $sql .= "}";
                    if (0 != $cnt){
                        $sql .= " $conn ";
                    }
                }
                return $this->db->query($sql, array_merge(array_values($change), array_values($condition)));
            }
            elseif(null == $condition){
                $sql .= "TRUE";
                return $this->db->query($sql, array_values($change));
            }
            else{
                return false;
            }
        }
        
        public function select($fields = null, $condition = null, $conn = 'and'){
            if (empty($this->table_name)) throw new Exception('table_name var not set!');
            if (empty($fields)){
                $fields = '*';
            }
            else{
                if (!is_array($fields)){
                    $fields = array($fields);
                }
                $fields = implode(', ', $fields);
            }
            $sql = "select $fields from " . $this->table_name . ' where ';
            if (is_array($condition) && !empty($condition)){
                $keys = array_keys($condition);
                $cnt = count($keys);
                while ($cnt --> 0){
                    $sql .= $keys[$cnt];
                    $sql .= " = {";
                    $sql .= "$cnt}";
                    if (0 != $cnt){
                        $sql .= " $conn ";
                    }
                }
                return $this->db->fetch($sql, array_values($condition));
            }
            else{
                $sql .= 'TRUE';
                return $this->db->fetch($sql);
            }                                                           
        }
    }
?>
